package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController // @ResponseBody + @Controller
@RequestMapping("admin/product")
public class SpuManageController {

    @Autowired
    private ManageService manageService;

    @GetMapping("{page}/{limit}")
    public Result getSpuList(@PathVariable Long page,
                             @PathVariable Long limit,
                             SpuInfo spuInfo){
        Page<SpuInfo> spuInfoPage = new Page<>(page,limit);
        IPage<SpuInfo> infoIPage = this.manageService.getSpuList(spuInfoPage,spuInfo);
        return Result.ok(infoIPage);
    }
    //获取所有销售属性
    @GetMapping("baseSaleAttrList")
    public Result getBaseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrList =  this.manageService.getBaseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //保存
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        this.manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    //根据平台属性值查询图片
    @GetMapping("spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){
          List<SpuImage> spuImageList = this.manageService.getSpuImageList(spuId);
          return Result.ok(spuImageList);
    }

    //根据spuId 查询销售属性集合
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = this.manageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }
    //根据spuId查询SpuInfo
    @GetMapping("getSpuInfo/{spuId}")
    public Result getSpuInfo(@PathVariable Long spuId){
        SpuInfo spuInfo = this.manageService.getSpuInfo(spuId);
        return Result.ok(spuInfo);
    }
    //修改Spu
    @PostMapping("updateSpuInfo")
    public Result updateSpuInfo(@RequestBody SpuInfo spuInfo){
        this.manageService.saveSpuInfo(spuInfo);
        List<SkuInfo> skuInfoList = this.manageService.getSkuInfoListTwo(spuInfo.getId());
        for (SkuInfo skuInfo : skuInfoList) {
            //this.manageService.saveSkuInfo(skuInfo);
            this.manageService.updateSkuInfo(skuInfo);
        }
        return Result.ok();
    }
}
